package com.zmn.mcc.services.impl.staff.stafffamily;

import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.mcc.model.entity.staff.dingtalk.StaffDingTalk;
import com.zmn.mcc.model.entity.staff.stafffamily.StaffFamily;
import com.zmn.mcc.model.entity.staff.stafffamily.StaffFamilyQuery;
import com.zmn.mcc.persistence.interfaces.staff.stafffamily.StaffFamilyDAO;
import com.zmn.mcc.services.interfaces.staff.stafffamily.StaffFamilyService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 描述: 家庭关系和联系人基础接口实现类
 *
 * @author tanghongjun
 * @since 2020-05-14 10:21:58
 **/
@Slf4j
@Service
public class StaffFamilyServiceImpl implements StaffFamilyService {

    private static final String TAG = "家庭关系和联系人基础服务接口";

    @Resource
    private StaffFamilyDAO staffFamilyDao;

    /**
     * 描述: 根据id查询数据
     *
     * @param familyId id
     * @author tanghongjun
     * @since 2020-05-14 10:21:58
     **/
    @Override
    public StaffFamily findById(Integer familyId) {

        if (Objects.isNull(familyId)) {
            log.error("[{}],根据id查询数据familyId为null", TAG);
            return null;
        }
        return staffFamilyDao.findById(familyId);
    }

    /**
     * 描述: 按条件查询数据 支持分页
     *
     * @param query 查询对象
     * @author tanghongjun
     * @since 2020-05-14 10:21:58
     **/
    @Override
    public List<StaffFamily> listPageByQuery(StaffFamilyQuery query) {

        if (Objects.isNull(query)) {
            log.error("[{}],按条件查询数据,query为null", TAG);
            return null;
        }
        int count = countByQuery(query);
        query.setTotalCount(count);
        return staffFamilyDao.listPageByQuery(query);
    }

    /**
     * 描述: 按条件查询数据 不支持分页
     *
     * @param query 查询对象
     * @author tanghongjun
     * @since 2020-05-14 10:21:58
     **/
    @Override
    public List<StaffFamily> listByQuery(StaffFamilyQuery query) {

        if (Objects.isNull(query)) {
            log.error("[{}],按条件查询数据,query为null", TAG);
            return null;
        }
        return staffFamilyDao.listByQuery(query);
    }

    /**
     * 描述: 按条件查询数据 不支持分页
     *
     * @param query 查询对象
     * @author tanghongjun
     * @since 2020-05-14 10:21:58
     **/
    @Override
    public Integer countByQuery(StaffFamilyQuery query) {

        if (Objects.isNull(query)) {
            log.error("[{}],按条件统计数据,query为null", TAG);
            return null;
        }
        return staffFamilyDao.countByQuery(query);
    }


    /**
     * 描述: 根据id修改数据 id为必传
     *
     * @param staffFamily 待修改数据
     * @return java.lang.Integer
     * @author tanghongjun
     * @since 2020-05-14 10:21:58
     **/
    @Override
    public Integer updateStaffFamilyByKey(StaffFamily staffFamily) {

        if (Objects.isNull(staffFamily.getFamilyId())) {
            log.error("[{}],根据id修改数据,staffFamily为null", TAG);
            return null;
        }
        return staffFamilyDao.updateStaffFamilyByKey(staffFamily);
    }

    @Override
    public Integer synchroByDingTalk(Integer staffId, StaffDingTalk staffDingTalk) {
        StaffFamilyQuery staffFamilyQuery = new StaffFamilyQuery();
        staffFamilyQuery.setStaffId(staffId);
        staffFamilyQuery.setMobile(staffDingTalk.getFamilyMobile());
        List<StaffFamily> staffFamilyList = staffFamilyDao.listByQuery(staffFamilyQuery);
        StaffFamily staffFamily = staffFamilyList.size() > 0 ? staffFamilyList.get(0) : new StaffFamily();
        staffFamily.setStaffId(staffId);
        if (StringUtils.isEmpty(staffFamily.getFamilyName()) && Objects.nonNull(staffDingTalk.getFamilyName())) {
            staffFamily.setFamilyName(staffDingTalk.getFamilyName());
        }
        if (StringUtils.isEmpty(staffFamily.getMobile()) && Objects.nonNull(staffDingTalk.getFamilyMobile())) {
            staffFamily.setMobile(staffDingTalk.getFamilyMobile());
        }
        if (Objects.isNull(staffFamily.getRelation()) && Objects.nonNull(staffDingTalk.getFamilyRelation())) {
            staffFamily.setRelation(staffDingTalk.getFamilyRelation());
        }
        staffFamily.setEmergencyContact(GlobalConsts.YES);
        if (NumberUtil.isNullOrZero(staffFamily.getFamilyId())) {
            return staffFamilyDao.insert(staffFamily);
        }
        return staffFamilyDao.updateStaffFamilyByKey(staffFamily);

    }

    /**
     * 描述: 新增数据
     *
     * @param staffFamily 待新增数据
     * @return java.lang.Integer 主键key
     * @author tanghongjun
     * @since 2020-05-14 10:21:58
     **/
    @Override
    public Integer insert(StaffFamily staffFamily) {

        if (Objects.isNull(staffFamily)) {
            log.error("[{}],新增数据,staffFamily为null", TAG);
            return null;
        }
        return staffFamilyDao.insert(staffFamily);
    }

    /**
     * 描述: 根据主键删除数据
     *
     * @param familyId
     * @return: 执行结果码
     * @auther: tanghongjun
     * @since: 2020-05-14 10:21:58
     */
    @Override
    public Integer deleteByKey(Integer familyId) {

        if (Objects.isNull(familyId)) {
            log.error("[{}],根据id删除数据,familyId为null", TAG);
            return null;
        }
        return staffFamilyDao.deleteByKey(familyId);
    }

}